package com.example.newdemo;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.newdemo.adapter.MainIndexAdapter;
import com.example.newdemo.animator.AnimatorDemoActivity;
import com.example.newdemo.base.SerializableTestDemo;
import com.example.newdemo.bean.BaseResultBean;
import com.example.newdemo.bean.ClassDataBean;
import com.example.newdemo.bean.IndexBean;
import com.example.newdemo.bean.LoginBean;
import com.example.newdemo.list.SparaseArrayDemo;
import com.example.newdemo.net.OkHttpInitDemo;
import com.example.newdemo.thread.ThreadActivity;
import com.example.newdemo.tv.FocusMoveDemoActivity;
import com.example.newdemo.tv.RecyclerTVDemoActivity;
import com.example.newdemo.utils.DipPxUtils;
import com.example.newdemo.utils.GetJsonFileDataUtil;
import com.example.newdemo.utils.LogUtils;
import com.example.newdemo.utils.permiss.RxPermissions;
import com.example.newdemo.view.ShowViewFragmentActivity;
import com.example.newdemo.view.ViewIndexActivity;
import com.example.newdemo.view.group.FlexViewActivity;
import com.example.newdemo.view.image.SvgImageActivity;
import com.example.newdemo.view.recyclerView.RecyclerViewActivity;
import com.example.newdemo.view.recyclerView.demo1.DividerItemDecoration;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;

public class MainActivity extends AppCompatActivity {

    private LinearLayoutManager linearLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_new);

//        initDemo();
//        sparaseArrayDemo();
//        netDemo();

//        readJsonDemo();
        getLoginData();
        initView();
        getPermission(this);
    }


    public  void getPermission(Activity context){
        RxPermissions rxPermissions = new RxPermissions(context);

        rxPermissions.request(Manifest.permission.CAMERA,
                Manifest.permission.RECORD_AUDIO)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean){
                            // 获取全部权限成功
                        }else{
                            // 获取全部权限失败
                        }
                    }
                });

    }

    private void readJsonDemo() {


        try {
            String jsonString = GetJsonFileDataUtil.getJson(this,"firstPage.json");
            JSONObject jsonData=new JSONObject(jsonString);
//            Gson gson = new Gson();
//            BaseResultBean<List<BaseResultBean>> resultBean = gson.fromJson(String.valueOf(jsonData), BaseResultBean.class);


            String arrayString = (jsonData.get("data")).toString();

            LogUtils.d(arrayString);
            List<ClassDataBean> list =  GetJsonFileDataUtil.getObjectList(arrayString,ClassDataBean.class);
            LogUtils.d(jsonData.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }


    private void getLoginData() {


        try {
            String jsonString = GetJsonFileDataUtil.getJson(this,"login.json");
            JSONObject jsonData=new JSONObject(jsonString);


            Gson gson = new Gson();
            BaseResultBean<LoginBean> resultBean = gson.fromJson(String.valueOf(jsonData), BaseResultBean.class);

            LogUtils.d(resultBean.toString());

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    private void netDemo() {
        OkHttpInitDemo demo = new OkHttpInitDemo();
        demo.httpInit();
    }


    private void sparaseArrayDemo() {
        SparaseArrayDemo.testDemo();
    }

    private void initDemo() {
        SerializableTestDemo demo = new SerializableTestDemo();
        demo.myParcelTest();
    }

    public void goTVDemo(View view) {
        Intent recyclerViewIntent = new Intent(MainActivity.this, RecyclerTVDemoActivity.class);
        startActivity(recyclerViewIntent);
    }

    public void goAnimator(View view) {
        Intent recyclerViewIntent = new Intent(MainActivity.this, AnimatorDemoActivity.class);
        startActivity(recyclerViewIntent);
    }

    public void goViewIndex(View view) {
        Intent recyclerViewIntent = new Intent(MainActivity.this, ViewIndexActivity.class);
        startActivity(recyclerViewIntent);
    }

    public void goSvg(View view) {
        Intent recyclerViewIntent = new Intent(MainActivity.this, SvgImageActivity.class);
        startActivity(recyclerViewIntent);
    }

    public void goRecyclerView(View view) {
        Intent recyclerViewIntent = new Intent(MainActivity.this, RecyclerViewActivity.class);
        startActivity(recyclerViewIntent);
    }

    public void goThreadDemo(View view) {
        Intent recyclerViewIntent = new Intent(MainActivity.this, ThreadActivity.class);
        startActivity(recyclerViewIntent);
    }

    public void goFlexView(View view) {
        Intent recyclerViewIntent = new Intent(MainActivity.this, FlexViewActivity.class);
        startActivity(recyclerViewIntent);
    }


    public void onBook() {
        Intent bookIntent = new Intent(MainActivity.this, BookActivity.class);
        startActivity(bookIntent);
    }

    @SuppressLint("ResourceAsColor")
    void initView() {

        SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swipe_refresh_layout_main_index);





        RecyclerView recyclerView = findViewById(R.id.recycler_main_index);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);


        int firstVisiblePosition = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
        int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
        int lastVisiblePosition = linearLayoutManager.findLastCompletelyVisibleItemPosition();
        int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();



        ArrayList<IndexBean> indexList =  getIndexList();
        MainIndexAdapter adapter = new MainIndexAdapter(MainActivity.this, indexList);
        recyclerView.setAdapter(adapter);


        DividerItemDecoration decoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST);

//        MyItemDecoration decoration = new MyItemDecoration(this);
        recyclerView.addItemDecoration(decoration);

        adapter.setOnItemStateListener(new MainIndexAdapter.OnItemStateIndexListener() {
            @Override
            public void onItemClick(View view, int position) {
                IndexBean bean = indexList.get(position);

                Intent recyclerViewIntent = new Intent(MainActivity.this,bean.actClass);
                startActivity(recyclerViewIntent);
            }
        });


        swipeRefreshLayout.setColorSchemeResources(R.color.red);

        swipeRefreshLayout.setProgressBackgroundColorSchemeColor(R.color.colorPrimaryDark);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipeRefreshLayout.setRefreshing(false);
                    }
                },2000);

            }
        });



    }


    RecyclerView.OnScrollListener recyclerScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {

            int firstVisiblePosition = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
            int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
            int lastVisiblePosition = linearLayoutManager.findLastCompletelyVisibleItemPosition();
            int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();

//            LogUtils.d("firstVisiblePosition = " +firstVisiblePosition +
//                    "; firstVisibleItemPosition = " + firstVisibleItemPosition +
//                    " ; lastVisiblePosition = " + lastVisiblePosition +
//                    " ; lastVisibleItemPosition = " + lastVisibleItemPosition );

            super.onScrollStateChanged(recyclerView, newState);
        }

        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
        }
    };

    public ArrayList<IndexBean> getIndexList() {
        ArrayList<IndexBean> indexList = new ArrayList<>();
        //com.example.newdemo.fragment.ImageFragmentActivity

        try {
            Class imageFragmentActivityClass = Class.forName("com.example.newdemo.fragment.ImageFragmentActivity");

            indexList.add(new IndexBean("popupWindo", ShowViewFragmentActivity.class));
            indexList.add(new IndexBean("fragment", imageFragmentActivityClass));
            indexList.add(new IndexBean("焦点移动", FocusMoveDemoActivity.class));
            indexList.add(new IndexBean("goTVDemo", RecyclerTVDemoActivity.class));

            indexList.add(new IndexBean("动画", AnimatorDemoActivity.class));

            indexList.add(new IndexBean("去Thread指导页面", ThreadActivity.class));

            indexList.add(new IndexBean("去view指导页面", ViewIndexActivity.class));

            indexList.add(new IndexBean("SVG解析", SvgImageActivity.class));

            indexList.add(new IndexBean("去recycler_view", RecyclerViewActivity.class));


            indexList.add(new IndexBean("去自动换行的view", FlexViewActivity.class));



//        indexList.add(new IndexBean("",.class));
//        indexList.add(new IndexBean("",.class));

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return indexList;
    }


    private void getScreenInfoTest() {

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        // 通过Resources获取
        // 获取屏幕的默认分辨率

        Display display = getWindowManager().getDefaultDisplay();
//        System.out.println("width-display :" + display.getWidth());
//        System.out.println("heigth-display :" + display.getHeight());

//        Toast.makeText(this,"dpi"+Integer.toString(metrics.densityDpi),Toast.LENGTH_SHORT).show();
//        Toast.makeText(this,"dpixx"+Double.toString(metrics.density),Toast.LENGTH_SHORT).show();
//
//        Toast.makeText(this,"widthPixels"+Double.toString(metrics.widthPixels),Toast.LENGTH_SHORT).show();
//        Toast.makeText(this,"hightPixels"+Double.toString(metrics.heightPixels),Toast.LENGTH_SHORT).show();

        LogUtils.display("dpi = "+Integer.toString(metrics.densityDpi),
                "dpixx  = "+Double.toString(metrics.density),
                "widthPixels  = "+Double.toString(metrics.widthPixels),
                "hightPixels  = "+Double.toString(metrics.heightPixels),
                "width-display :" + display.getWidth(),
                "heigth-display :" + display.getHeight()
        );
    }

    private void measureScreenDpPxTest() {

        View measureView = findViewById(R.id.view_measure_fit);

        int displayWidth = getWindowManager().getDefaultDisplay().getWidth();
        int displayHeight  = getWindowManager().getDefaultDisplay().getHeight();

//        ViewGroup.LayoutParams params = measureView.getLayoutParams();
//        int viewWidth = params.width;
//        int height = params.height;
//
//        LogUtils.i("viewWidth = "+ viewWidth +"; height = "+ height);
//
////        params.width = viewWidth * 2;
////        params.height = height * 2;
//
//        measureView.setLayoutParams(params);



//        // displayWidth = 1080 ; displayHeight = 2206
//        LogUtils.e(" displayWidth = " +displayWidth +" ; displayHeight = " + displayHeight);
////
////
//        // dp : displayWidth = 840 ; dp : displayHeight = 411
//        LogUtils.e("dp : displayWidth = " + DipPxUtils.px2dip(LoginActivity.this,displayWidth)  +
//                " ; dp : displayHeight = " + DipPxUtils.px2dip(LoginActivity.this,displayHeight) );
//
        measureView.post(new Runnable() {
            @Override
            public void run() {
                int width = measureView.getWidth();
                int height = measureView.getHeight();
                int measureWidth = measureView.getMeasuredWidth();
                int measuredHeight = measureView.getMeasuredHeight();

                LogUtils.e("100 width  = " +width +"; height = " + height +" ; measureWidth = " + measureWidth +" ; measureHeight = " + measuredHeight);
                LogUtils.e(" 100 dp  : width = " + DipPxUtils.px2dip(MainActivity.this,width) +
                        "; height = " + DipPxUtils.px2dip(MainActivity.this,height) +
                        " ; measureWidth = " + DipPxUtils.px2dip(MainActivity.this,measureWidth)  +
                        " ; measureHeight = " + DipPxUtils.px2dip(MainActivity.this,measuredHeight) );
            }
        });

    }

    private void measureMatchViewTest() {

//        View measureView = findViewById(R.id.view_measure_match_fit);
//
//        measureView.post(new Runnable() {
//            @Override
//            public void run() {
//                int width = measureView.getWidth();
//                int height = measureView.getHeight();
//                int measureWidth = measureView.getMeasuredWidth();
//                int measuredHeight = measureView.getMeasuredHeight();
//
//                LogUtils.e("match width = " +width +"; height = " + height +" ; measureWidth = " + measureWidth +" ; measureHeight = " + measuredHeight);
//                LogUtils.e("match  dp  : width = " + DipPxUtils.px2dip(LoginActivity.this,width) +
//                        "; height = " + DipPxUtils.px2dip(LoginActivity.this,height) +
//                        " ; measureWidth = " + DipPxUtils.px2dip(LoginActivity.this,measureWidth)  +
//                        " ; measureHeight = " + DipPxUtils.px2dip(LoginActivity.this,measuredHeight) );
//            }
//        });


    }


}